/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package neo.workforce.controller;

import java.util.Calendar;
import java.util.LinkedHashMap;
import java.util.Map;
import neo.workforce.dao.ActivityDAO;
import neo.workforce.dao.ProcessDAO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author krisada
 */
@Controller
public class ActivityController {

    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private ActivityDAO activityDao = null;
    
    @Autowired
     private ProcessDAO processDao = null;


    public ActivityController() {
    }

    public ActivityDAO getActivityDao() {
        return activityDao;
    }

    public void setActivityDao(ActivityDAO activityDao) {
        this.activityDao = activityDao;
    }
    
    public ProcessDAO getProcessDao() {
        return processDao;
    }

    public void setProcessDao(ActivityDAO activityDao) {
        this.activityDao = activityDao;
    }

    @RequestMapping(value = "/activityEntry", method = RequestMethod.POST)
    public ModelAndView processEntry(@ModelAttribute("SpringWeb") neo.workforce.dao.Process process, ModelMap model) {
        neo.workforce.dao.Activity activity = new neo.workforce.dao.Activity();
        activity.setProcess(process);
        model.addAttribute("mode_action", "addActivity.htm");
        model.addAttribute("processId", process.getId());
        Map<String, String> states = new LinkedHashMap<String, String>();
        states.put("START", "Start");
        states.put("FOLLOW", "Follow");
        states.put("STOP", "Stop");
        model.addAttribute("states", states);
        return new ModelAndView("activity_entry", "command", activity);
    }
    
    @RequestMapping(value = "/addActivity", method = RequestMethod.POST)
    public String addProcess(@ModelAttribute("SpringWeb") neo.workforce.dao.Activity activity,
            ModelMap model) {
        activity.setCreateDate(new java.util.Date(Calendar.getInstance().getTimeInMillis()));
        
        neo.workforce.dao.Process process = processDao.find(new Long(activity.getProcess().getId()));
        activity.setProcess(process);
        long id = activityDao.save(activity);
        model.addAttribute("id", new Long(id));
        model.addAttribute("activityId", process.getProcessId());
        model.addAttribute("name", process.getName());
        model.addAttribute("description", process.getDescription());
        return "result_activity";
    }
}
